using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace CatalogMigration
{
	/// <summary>
	/// Summary description for Relation.
	/// </summary>
	public class Relation : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.Panel panel3;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.CheckedListBox checkedListBox1;
		private System.Windows.Forms.CheckedListBox checkedListBox2;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Relation()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.panel1 = new System.Windows.Forms.Panel();
			this.panel2 = new System.Windows.Forms.Panel();
			this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
			this.label1 = new System.Windows.Forms.Label();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.panel3 = new System.Windows.Forms.Panel();
			this.checkedListBox2 = new System.Windows.Forms.CheckedListBox();
			this.label2 = new System.Windows.Forms.Label();
			this.panel1.SuspendLayout();
			this.panel2.SuspendLayout();
			this.panel3.SuspendLayout();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(56, 8);
			this.button1.Name = "button1";
			this.button1.TabIndex = 0;
			this.button1.Text = "OK";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(144, 8);
			this.button2.Name = "button2";
			this.button2.TabIndex = 1;
			this.button2.Text = "Cancel";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.button2);
			this.panel1.Controls.Add(this.button1);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel1.Location = new System.Drawing.Point(0, 269);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(288, 40);
			this.panel1.TabIndex = 2;
			// 
			// panel2
			// 
			this.panel2.Controls.Add(this.checkedListBox1);
			this.panel2.Controls.Add(this.label1);
			this.panel2.Dock = System.Windows.Forms.DockStyle.Left;
			this.panel2.Location = new System.Drawing.Point(0, 0);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(136, 269);
			this.panel2.TabIndex = 3;
			// 
			// checkedListBox1
			// 
			this.checkedListBox1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.checkedListBox1.Location = new System.Drawing.Point(0, 23);
			this.checkedListBox1.Name = "checkedListBox1";
			this.checkedListBox1.Size = new System.Drawing.Size(136, 244);
			this.checkedListBox1.TabIndex = 1;
			this.checkedListBox1.SelectedIndexChanged += new System.EventHandler(this.checkedListBox1_SelectedIndexChanged);
			// 
			// label1
			// 
			this.label1.Dock = System.Windows.Forms.DockStyle.Top;
			this.label1.Location = new System.Drawing.Point(0, 0);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(136, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "Category Fields";
			// 
			// splitter1
			// 
			this.splitter1.Location = new System.Drawing.Point(136, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 269);
			this.splitter1.TabIndex = 4;
			this.splitter1.TabStop = false;
			// 
			// panel3
			// 
			this.panel3.Controls.Add(this.checkedListBox2);
			this.panel3.Controls.Add(this.label2);
			this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel3.Location = new System.Drawing.Point(139, 0);
			this.panel3.Name = "panel3";
			this.panel3.Size = new System.Drawing.Size(149, 269);
			this.panel3.TabIndex = 5;
			// 
			// checkedListBox2
			// 
			this.checkedListBox2.Dock = System.Windows.Forms.DockStyle.Fill;
			this.checkedListBox2.Location = new System.Drawing.Point(0, 23);
			this.checkedListBox2.Name = "checkedListBox2";
			this.checkedListBox2.Size = new System.Drawing.Size(149, 244);
			this.checkedListBox2.TabIndex = 1;
			this.checkedListBox2.SelectedIndexChanged += new System.EventHandler(this.checkedListBox2_SelectedIndexChanged);
			// 
			// label2
			// 
			this.label2.Dock = System.Windows.Forms.DockStyle.Top;
			this.label2.Location = new System.Drawing.Point(0, 0);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(149, 23);
			this.label2.TabIndex = 0;
			this.label2.Text = "Products Fields";
			// 
			// Relation
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(288, 309);
			this.Controls.Add(this.panel3);
			this.Controls.Add(this.splitter1);
			this.Controls.Add(this.panel2);
			this.Controls.Add(this.panel1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "Relation";
			this.Text = "Relate Categories to Products";
			this.Load += new System.EventHandler(this.Relation_Load);
			this.panel1.ResumeLayout(false);
			this.panel2.ResumeLayout(false);
			this.panel3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e)
		{
			// set the fields
			frmCatalogMigrationTool.productField = checkedListBox2.SelectedItem.ToString();
			frmCatalogMigrationTool.categoryField = checkedListBox1.SelectedItem.ToString();
			Tools.Log(@"[Info] Product and Category fields selected");
			this.Close();
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void Relation_Load(object sender, System.EventArgs e)
		{
			// load the fields for the products and categoreis table
			getFields(frmCatalogMigrationTool.categoryTable, checkedListBox1);
			getFields(frmCatalogMigrationTool.productTable, checkedListBox2);
		}

		private void getFields(string tableName, CheckedListBox selection)
		{
			try
			{
				string SQLString = "SELECT syscolumns.name "+
					"FROM sysobjects INNER JOIN "+
					"syscolumns ON sysobjects.id = syscolumns.id "+
					"WHERE (sysobjects.xtype = 'u') "+
					"AND (sysobjects.name = N'"+tableName+"')";
				SqlConnection sqlcon = new SqlConnection(frmCatalogMigrationTool.ConStr);
				sqlcon.Open();
				SqlCommand cmd = sqlcon.CreateCommand();

				cmd.CommandText = SQLString;
				cmd.Connection = sqlcon;
				SqlDataReader rdr = cmd.ExecuteReader();

				while(rdr.Read())
				{
					selection.Items.Add(rdr.GetValue(0).ToString());
				}

				sqlcon.Close();
			}
			catch(Exception ex)
			{
				Tools.Log(@"[Fail] Error while getting sql fields", ex);
			}
		}

		private void checkedListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// make sure that the user can only select one field
			selectOnCheckBox(checkedListBox1);
		}

		private void checkedListBox2_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// make sure that the user can only select one field
			selectOnCheckBox(checkedListBox2);
		}

		private void selectOnCheckBox(CheckedListBox selection)
		{
			// only allow one checkbox to be selected.
			// get the current selected item
			int currentChkItem = selection.SelectedIndex;
			
			// iterate and uncheck all items
			for(int i = 0; selection.Items.Count > i; i++)
			{
				selection.SetItemChecked(i, false);
			}

			// only set what was selected
			selection.SetItemChecked(currentChkItem, true);
		}
	}
}
